<?php

namespace app\admin\controller;

use app\admin\common\Base;

/**
 * 站点信息控制器
 * Class Site
 * @package app\admin\controller
 */
class Site extends Base {
    public function index(){
        $info = db('site')->where('id',1)->find();
        $this->assign('info',$info);

        return $this->fetch();
    }

    public function edit(){
        if (is_post()){
            $data = input('post.');
            $data['update_time'] = time();
            unset($data['file']);
            $result = db('site')
                ->where('id',1)
                ->data($data)
                ->update();

            if ($result){
                insert_system_log('修改站点信息');
                return json(['code' => 'suc','msg' => '站点信息修改成功']);
            }else{
                return json(['code' => 'err','msg' => '站点信息修改失败，请重试']);
            }
        }
    }

    public function upload(){
        $file = request()->file('file');
        $info = $this->uploadsImg($file,'uploads/site/');
        if ($info !== false){
            return json(['code' => 'suc','msg' => '图片上传成功','url' => $info['path']]);
        }else{
            return json(['code' => 'err','msg' => '图片上传失败，请重试']);
        }
    }

    public function faviconUpload(){
        $file = request()->file('file');
        $config = [
            'size' => 5242880,//最大允许上传5M
        ];
        $save_path = 'uploads/site/';
        $info = $file
            ->validate($config)
            ->move($save_path);
        if ($info !== false){
            return json(['code' => 'suc','msg' => '图片上传成功','url' => '/' . $save_path . $info->getFilename()]);
        }else{
            return json(['code' => 'err','msg' => '图片上传失败，请重试']);
        }
    }
}